package com.spring.security.social.weixin.config;

import com.spring.security.properties.SecurityProperties;
import com.spring.security.social.HkConnectView;
import com.spring.security.social.qq.connet.QQConnectionFactory;
import com.spring.security.social.weixin.connect.WeixinConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.social.config.annotation.ConnectionFactoryConfigurer;
import org.springframework.social.config.annotation.SocialConfigurerAdapter;

/**
 * 微信登录配置
 */
@Configuration
// 当配置了appId的时候才启用
@ConditionalOnProperty(prefix = "hk.security.social.weixin", name = "appId")
public class WeixinAutoConfig extends SocialConfigurerAdapter {
    @Autowired
    private SecurityProperties securityProperties;


    // 添加关于ProviderId=weixin的处理器。注意clientId和clientSecret需要自己去微信开发平台申请
    @Override
    public void addConnectionFactories(
            ConnectionFactoryConfigurer connectionFactoryConfigurer, Environment environment) {
        connectionFactoryConfigurer.addConnectionFactory(
                new WeixinConnectionFactory(securityProperties.getSocial().getWeixin().getProviderId(), securityProperties.getSocial().getWeixin().getAppId(), securityProperties.getSocial().getWeixin().getAppSecret()));
    }

    /**
     * weixin绑定解绑界面视图
     * connect/weixinConnected:绑定
     * connect/weixinConnect:解绑
     *
     * @return
     */
    @Bean({"connect/weixinConnect", "connect/weixinConnected"})
    @ConditionalOnMissingBean(name = "weixinConnectedView") //可以自定义实现覆盖此默认界面
    public HkConnectView weixinConnectedView() {
        return new HkConnectView();
    }

}
